package com.car.loan.framework.aspectj.impl;

import com.car.loan.common.enums.LoanStatusEnum;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.aspectj.DataStatusSqlService;
import com.car.loan.project.xr.util.bo.business.LoanBaseInfo;
import com.car.loan.project.xr.util.enums.BusinessApostilleStatus;
import com.car.loan.project.xr.util.enums.DataAuditStatus;
import com.car.loan.project.xr.util.enums.FileAuditStatus;
import com.car.loan.project.xr.util.enums.LoanBusinessAuditStatus;
import com.car.loan.project.xr.util.enums.LoanBusinessStatus;
import com.car.loan.project.xr.util.enums.MakeLoansInfoStatus;
import org.springframework.stereotype.Service;

/**
 * @author huangding
 * @description 业务状态
 * @date 2019/11/9 16:07
 */
@Service
public class BusinessStatusSqlServiceImpl implements DataStatusSqlService {

    @Override
    public LoanStatusEnum getLoanStatusEnum() {
        return LoanStatusEnum.BUSINESS_STATUS;
    }

    @Override
    public LoanBaseInfo getStatusRemark(LoanBaseInfo loanBaseInfo) {
        Long businessStatus = loanBaseInfo.getBusinessStatus();
        Long businessAuditStatus = loanBaseInfo.getBusinessAuditStatus();
        Long businessApostilleStatus = loanBaseInfo.getBusinessApostilleStatus();
        String statusRemark = StringUtils.EMPTY;
        if (businessStatus == LoanBusinessStatus.INIT.status) {
            statusRemark = "待提交";
        } else if (LoanBusinessAuditStatus.INIT.status == businessAuditStatus) {
            statusRemark = "待审批";
        } else if (LoanBusinessAuditStatus.APOSTILLE.status == businessAuditStatus &&
            businessApostilleStatus == BusinessApostilleStatus.INIT.status) {
            statusRemark = "待加签";
        } else if (businessAuditStatus == LoanBusinessAuditStatus.PASS.status ||
            businessApostilleStatus == BusinessApostilleStatus.PASS.status) {
            statusRemark = "已审批";
        } else if (businessAuditStatus == LoanBusinessAuditStatus.GO_BACK.status) {
            statusRemark = "审批退回";
        } else if (businessApostilleStatus == BusinessApostilleStatus.GO_BACK.status) {
            statusRemark = "加签退回";
        } else if (businessAuditStatus == LoanBusinessAuditStatus.REJECT.status) {
            statusRemark = "审批拒绝";
        } else if (businessApostilleStatus == BusinessApostilleStatus.REJECT.status) {
            statusRemark = "加签拒绝";
        }
        loanBaseInfo.setStatusRemark(statusRemark);
        return loanBaseInfo;
    }

    @Override
    public String getSql(String statusAlias, Integer status) {
        String sql = " ({}.make_loans_info_status = "
            + MakeLoansInfoStatus.INIT.status
            + " or {}.data_audit_status =  " + DataAuditStatus.INIT.status + "))";
        if (status != null) {
            //0待提交1待审批2待加签3已审批4已退回5已拒绝
            //
            //待审批(业务申请已提交且业务审批未提交）
            //
            //已审批（业务审批同意或业务审批加签且加签审批同意）
            //已退回（业务审批退回或加签审批退回）
            //
            if (0 == status) {
                //待提交(业务申请未提交，默认） +
                sql += " and (({}.business_status =  " + LoanBusinessStatus.INIT.status + " or"
                    + " ( {}.business_status =  " + LoanBusinessStatus.SUBMIT.status
                    + " and( {}.business_audit_status=" + LoanBusinessAuditStatus.GO_BACK.status
                    + " or {}.business_apostille_status=" + BusinessApostilleStatus.GO_BACK.status
                    + " )))"
                    + " or ( {}.business_status =  " + LoanBusinessStatus.SUBMIT.status
                    + " and(( {}.business_audit_status=" + LoanBusinessAuditStatus.REJECT.status
                    + " or {}.business_apostille_status=" + BusinessApostilleStatus.REJECT.status
                    + " )))";

            } else if (1 == status) {
                //待审批(业务申请已提交且业务审批未提交） +加簽
                sql += " and ({}.business_status =  " + LoanBusinessStatus.SUBMIT.status + " and "
                    + " ( ( {}.business_audit_status =  " + LoanBusinessAuditStatus.INIT.status
                    + " or {}.business_audit_status=" + LoanBusinessAuditStatus.APOSTILLE.status
                    + " ) and {}.business_apostille_status=" + BusinessApostilleStatus.INIT.status
                    + " )";
            } else if (2 == status) {
                //把已拒绝改为已审批，用来展示已拒绝和已审批
                sql += " and ( {}.business_audit_status=" + LoanBusinessAuditStatus.PASS.status
                    + " or {}.business_apostille_status=" + BusinessApostilleStatus.PASS.status
                    + " ";
            }
        }
        return sql.replace("{}", statusAlias);
    }

    @Override
    public String getSortSql(String statusAlias) {
        return " order by loan.id  desc";
    }
}
